package com.ask.forMe.controller.ai;

import com.ask.forMe.langchain4j.service.EmbeddingService;
import com.ask.forMe.model.entity.Result;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.store.embedding.EmbeddingMatch;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/rag")
@Tag(name = "RAG接口")
public class RAGController {

    @Autowired
    private EmbeddingService embeddingService;

    @GetMapping("/hitRate")
    @Operation(summary = "命中率测试")
    public Result evaluateHitRate(String message) {
        EmbeddingMatch<TextSegment> embeddingMatch = embeddingService.embeddingSearch(message);
        return Result.success(embeddingMatch.embedded().text());
    }

    @GetMapping("/upload")
    @Operation(summary = "上传知识到向量数据库")
    public Result uploadKnowledge2Pinecone(@RequestParam List<String> fileNames, @RequestParam String path) {
        embeddingService.uploadKnowledge2Pinecone(path, fileNames);
        return Result.success();
    }


    @GetMapping("/qua")
    @Operation(summary = "问题答案查询")
    public Result querySimilarQuestions(String question) {
        String res = embeddingService.querySimilarQuestions(question);
        if (res == null)
            return Result.fail("null");
        return Result.success(res);
    }

    @PostMapping("/uploadAns")
    @Operation(summary = "上传问题答案")
    public Result uploadAns2Pinecone(String ans) {
        embeddingService.uploadAns2Pinecone(ans);
        return Result.success();
    }

    @GetMapping("/uploadAndFilter")
    @Operation(summary = "上传并过滤")
    public Result uploadAndFilter() {
        embeddingService.uploadAndFilter();
        return Result.success();
    }

}
